<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - gui_core_kernel_2.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2005  Davis E. King (davis@dlib.net), Keita Mochizuki
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_GUI_CORE_KERNEL_2_CPp_
<font color='#0000FF'>#define</font> DLIB_GUI_CORE_KERNEL_2_CPp_
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../platform.h.html'>../platform.h</a>"

<font color='#0000FF'>#ifdef</font> POSIX

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='gui_core_kernel_2.h.html'>gui_core_kernel_2.h</a>"


<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>X11<font color='#5555FF'>/</font>Xlib.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>X11<font color='#5555FF'>/</font>Xutil.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>X11<font color='#5555FF'>/</font>keysym.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>X11<font color='#5555FF'>/</font>Xlocale.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>X11<font color='#5555FF'>/</font>XKBlib.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>poll.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../assert.h.html'>../assert.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../queue.h.html'>../queue.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>cstring<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>cmath<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>X11<font color='#5555FF'>/</font>Xatom.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../sync_extension.h.html'>../sync_extension.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../logger.h.html'>../logger.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>vector<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>set<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../smart_pointers_thread_safe.h.html'>../smart_pointers_thread_safe.h</a>"

<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals
    <b>{</b>
        <font color='#0000FF'><u>void</u></font> <b><a name='init_keyboard_mod_masks'></a>init_keyboard_mod_masks</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>struct</font> <b><a name='user_event_type'></a>user_event_type</b>
        <b>{</b>
            Window w;
            <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> p;
            <font color='#0000FF'><u>int</u></font> i;
        <b>}</b>;

        <font color='#0000FF'>typedef</font> sync_extension<font color='#5555FF'>&lt;</font>queue<font color='#5555FF'>&lt;</font>user_event_type,memory_manager<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font>::kernel_1b<font color='#5555FF'>&gt;</font>::kernel_2a_c<font color='#5555FF'>&gt;</font>::kernel_1a queue_of_user_events;

        <font color='#0000FF'>typedef</font> sync_extension<font color='#5555FF'>&lt;</font>binary_search_tree<font color='#5555FF'>&lt;</font>Window,base_window<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font>::kernel_1a<font color='#5555FF'>&gt;</font>::kernel_1a 
            window_table_type;

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'>const</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>dlib::mutex<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> <b><a name='global_mutex'></a>global_mutex</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>static</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>dlib::mutex<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>m</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> dlib::mutex<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> m;
        <b>}</b>

        <font color='#0000FF'>class</font> <b><a name='event_handler_thread'></a>event_handler_thread</b> : <font color='#0000FF'>public</font> threaded_object
        <b>{</b>
        <font color='#0000FF'>public</font>:

            <font color='#0000FF'>enum</font> <b><a name='et_state'></a>et_state</b>
            <b>{</b>
                uninitialized,
                initialized,
                failure_to_init 
            <b>}</b>;

            et_state status;
            logger dlog;


            <font color='#0000FF'><u>int</u></font> depth;
            Display<font color='#5555FF'>*</font> disp;
            XIM xim;
            XIMStyle xim_style;
            Screen<font color='#5555FF'>*</font> screen;

            Atom delete_window; 
            Window exit_window;
            std::wstring clipboard;

            <font color='#0000FF'><u>int</u></font> alt_mask;
            <font color='#0000FF'><u>int</u></font> meta_mask;
            <font color='#0000FF'><u>int</u></font> num_lock_mask;
            <font color='#0000FF'><u>int</u></font> scroll_lock_mask;

            <font color='#009900'>// the mutex in this object is the global mutex used to protect everything
</font>            <font color='#009900'>// in the gui_core and gui_widgets components.
</font>            window_table_type window_table;

            rsignaler window_close_signaler;
            rsignaler et_signaler;

            queue_of_user_events user_events;
            queue_of_user_events user_events_temp;

            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>dlib::mutex<font color='#5555FF'>&gt;</font> reference_to_global_mutex;

            <b><a name='event_handler_thread'></a>event_handler_thread</b><font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font> :
                dlog<font face='Lucida Console'>(</font>"<font color='#CC0000'>dlib.gui_core</font>"<font face='Lucida Console'>)</font>,
                depth<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                disp<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                xim<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                screen<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                alt_mask<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                meta_mask<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                num_lock_mask<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                scroll_lock_mask<font face='Lucida Console'>(</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>,
                window_close_signaler<font face='Lucida Console'>(</font>window_table.get_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                et_signaler<font face='Lucida Console'>(</font>window_table.get_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                reference_to_global_mutex<font face='Lucida Console'>(</font>global_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

                status <font color='#5555FF'>=</font> uninitialized;

                <font color='#009900'>// start up the event handler thread
</font>                <font color='#BB00BB'>start</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                <font color='#009900'>// wait for the event thread to get up and running
</font>                <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> uninitialized<font face='Lucida Console'>)</font>
                    et_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> failure_to_init<font face='Lucida Console'>)</font>
                    <font color='#0000FF'>throw</font> <font color='#BB00BB'>gui_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Failed to initialize X11 resources</font>"<font face='Lucida Console'>)</font>;

                <font color='#BB00BB'>init_keyboard_mod_masks</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <b>}</b>

            ~<b><a name='event_handler_thread'></a>event_handler_thread</b> <font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
            <b>{</b>
                
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>is_alive</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>!</font><font color='#5555FF'>=</font> failure_to_init<font face='Lucida Console'>)</font>
                    <b>{</b>
                        XConfigureEvent event;
                        event.type <font color='#5555FF'>=</font> ConfigureNotify;
                        event.send_event <font color='#5555FF'>=</font> True;
                        event.display <font color='#5555FF'>=</font> disp;
                        event.window <font color='#5555FF'>=</font> exit_window;
                        event.x <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
                        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;
                        <font color='#BB00BB'>XPutBackEvent</font><font face='Lucida Console'>(</font>disp,<font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>event<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;

                        <font color='#009900'>// This should cause XNextEvent() to unblock so that it will see 
</font>                        <font color='#009900'>// this ConfigureNotify event we are putting onto the event queue.
</font>                        <font color='#BB00BB'>XSendEvent</font><font face='Lucida Console'>(</font>disp,exit_window,False,<font color='#979000'>0</font>,<font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>event<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;

                        <font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>xim <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
                        <b>{</b>
                            <font color='#BB00BB'>XCloseIM</font><font face='Lucida Console'>(</font>xim<font face='Lucida Console'>)</font>;
                        <b>}</b>

                        <font color='#BB00BB'>XCloseDisplay</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;


                    <b>}</b>
                    <font color='#0000FF'>else</font>
                    <b>{</b>

                        <font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                <b>}</b>


            <b>}</b>

        <font color='#0000FF'>private</font>:

            <font color='#0000FF'><u>void</u></font> <b><a name='thread'></a>thread</b> <font face='Lucida Console'>(</font>
            <font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
                <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
                <font color='#0000FF'>try</font>
                <b>{</b>

                    <font color='#009900'>// You are supposed to call this if using XLib in a threaded program.  Note
</font>                    <font color='#009900'>// however that at one point I noticed that calling this causes a dead-lock 
</font>                    <font color='#009900'>// when using XIM.  But I can't reproduce that anymore and not calling it 
</font>                    <font color='#009900'>// sometimes causes XCloseDisplay() to hang.
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>XInitThreads</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Unable to initialize threading support.</font>";
                        <font color='#009900'>// signal that an error has occurred
</font>                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        status <font color='#5555FF'>=</font> failure_to_init;
                        et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>return</font>;
                    <b>}</b>

                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    disp <font color='#5555FF'>=</font> <font color='#BB00BB'>XOpenDisplay</font><font face='Lucida Console'>(</font>NULL<font face='Lucida Console'>)</font>;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>disp <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                    <b>{</b>
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        disp <font color='#5555FF'>=</font> <font color='#BB00BB'>XOpenDisplay</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>:0.0</font>"<font face='Lucida Console'>)</font>;
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>disp <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                        <b>{</b>
                            dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Unable to connect to the X display.</font>";
                            <font color='#009900'>// signal that an error has occurred
</font>                            window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            status <font color='#5555FF'>=</font> failure_to_init;
                            et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'>return</font>;
                        <b>}</b>
                    <b>}</b>

                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    screen <font color='#5555FF'>=</font> <font color='#BB00BB'>DefaultScreenOfDisplay</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;
                    depth <font color='#5555FF'>=</font> <font color='#BB00BB'>DefaultDepthOfScreen</font><font face='Lucida Console'>(</font>screen<font face='Lucida Console'>)</font>;
                    delete_window <font color='#5555FF'>=</font> <font color='#BB00BB'>XInternAtom</font><font face='Lucida Console'>(</font>disp,"<font color='#CC0000'>WM_DELETE_WINDOW</font>",<font color='#979000'>1</font><font face='Lucida Console'>)</font>; 
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                    xim <font color='#5555FF'>=</font> NULL;
                    <font color='#009900'>// I'm disabling XIM usage all together because calling XSetICValues()
</font>                    <font color='#009900'>// in set_im_pos() randomly hangs the application (on Ubuntu 13.10 at
</font>                    <font color='#009900'>// least).    
</font>                    <font color='#009900'>/*
                    window_table.get_mutex().lock();
                    std::string saved_locale(setlocale (LC_CTYPE, NULL));
                    if (setlocale( LC_CTYPE, "" ) &amp;&amp; XSupportsLocale() &amp;&amp; XSetLocaleModifiers(""))
                        xim = XOpenIM(disp, NULL, NULL, NULL);
                    else
                        setlocale( LC_CTYPE, saved_locale.c_str() );
                    window_table.get_mutex().unlock();
                    */</font>
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>xim<font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'>const</font> <font color='#0000FF'>static</font> XIMStyle preedit_styles[] <font color='#5555FF'>=</font>
                            <b>{</b>XIMPreeditPosition, XIMPreeditNothing, XIMPreeditNone, <font color='#979000'>0</font><b>}</b>;
                        <font color='#0000FF'>const</font> <font color='#0000FF'>static</font> XIMStyle status_styles[] <font color='#5555FF'>=</font>
                            <b>{</b>XIMStatusNothing, XIMStatusNone, <font color='#979000'>0</font><b>}</b>;
                        xim_style <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

                        XIMStyles <font color='#5555FF'>*</font>xim_styles;
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                        <font color='#BB00BB'>XGetIMValues</font> <font face='Lucida Console'>(</font>xim, XNQueryInputStyle, <font color='#5555FF'>&amp;</font>xim_styles, <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>NULL<font face='Lucida Console'>)</font>;
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        std::set<font color='#5555FF'>&lt;</font>XIMStyle<font color='#5555FF'>&gt;</font> xims;
                        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> xim_styles<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>count_styles; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font><b>{</b>
                            xims.<font color='#BB00BB'>insert</font><font face='Lucida Console'>(</font>xim_styles<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>supported_styles[i]<font face='Lucida Console'>)</font>;
                        <b>}</b>
                        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> j <font color='#5555FF'>=</font> <font color='#979000'>0</font>; status_styles[j]; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font><b>{</b>
                            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; preedit_styles[i]; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font><b>{</b>
                                xim_style <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>status_styles[j] <font color='#5555FF'>|</font> preedit_styles[i]<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>xims.<font color='#BB00BB'>count</font><font face='Lucida Console'>(</font>xim_style<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#0000FF'>break</font>;
                            <b>}</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>xim_style<font face='Lucida Console'>)</font> <font color='#0000FF'>break</font>;
                        <b>}</b>
                        <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>xim_styles<font face='Lucida Console'>)</font>;
                    <b>}</b>

                    <font color='#009900'>// make this window just so we can send messages to it and trigger
</font>                    <font color='#009900'>// events in the event thread
</font>                    XSetWindowAttributes attr;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    exit_window <font color='#5555FF'>=</font> <font color='#BB00BB'>XCreateWindow</font><font face='Lucida Console'>(</font>
                        disp,
                        <font color='#BB00BB'>DefaultRootWindow</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>,
                        <font color='#979000'>0</font>,
                        <font color='#979000'>0</font>,
                        <font color='#979000'>10</font>,  <font color='#009900'>// this is the default width of a window
</font>                        <font color='#979000'>10</font>,  <font color='#009900'>// this is the default width of a window
</font>                        <font color='#979000'>0</font>,
                        depth,
                        InputOutput,
                        CopyFromParent,
                        <font color='#979000'>0</font>,
                        <font color='#5555FF'>&amp;</font>attr
                    <font face='Lucida Console'>)</font>;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                    <font color='#009900'>// signal that the event thread is now up and running
</font>                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    status <font color='#5555FF'>=</font> initialized;
                    et_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

                    <font color='#009900'>// start the event handler
</font>                    <font color='#BB00BB'>event_handler</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <b>}</b>
                <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>std::exception<font color='#5555FF'>&amp;</font> e<font face='Lucida Console'>)</font>
                <b>{</b>
                    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\nEXCEPTION THROWN: \n</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
                    <font color='#BB00BB'>abort</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <b>}</b>
                <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
                <b>{</b>
                    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>UNKNOWN EXCEPTION THROWN.\n</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
                    <font color='#BB00BB'>abort</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                <b>}</b>
            <b>}</b>

            <font color='#0000FF'><u>void</u></font> <b><a name='event_handler'></a>event_handler</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>void</u></font> <b><a name='init_keyboard_mod_masks'></a>init_keyboard_mod_masks</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>;

        <font color='#0000FF'>struct</font> <b><a name='x11_base_windowstuff'></a>x11_base_windowstuff</b>
        <b>{</b>
            Window hwnd;
            Time last_click_time;
            XIC xic;
            XFontSet fs;
            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> globals;
        <b>}</b>;

        <font color='#009900'>// Do all this just to make sure global_mutex() is initialized at program start
</font>        <font color='#009900'>// and thus hopefully before any threads have the chance to startup and call
</font>        <font color='#009900'>// global_data() concurrently.
</font>        <font color='#0000FF'>struct</font> <b><a name='call_global_mutex'></a>call_global_mutex</b> <b>{</b> <b><a name='call_global_mutex'></a>call_global_mutex</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <b>{</b> <font color='#BB00BB'>global_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <b>}</b> <b>}</b>;
        <font color='#0000FF'>static</font> call_global_mutex call_global_mutex_instance;

        <font color='#0000FF'>const</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> <b><a name='global_data'></a>global_data</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
        <b>{</b>
            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font color='#BB00BB'>global_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>static</font> shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> p;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>p.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                p.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> <font color='#BB00BB'>event_handler_thread</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>return</font> p;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        Bool <b><a name='XCheckIfEventPredicate'></a>XCheckIfEventPredicate</b> <font face='Lucida Console'>(</font>
            Display<font color='#5555FF'>*</font> ,
            XEvent<font color='#5555FF'>*</font> event,
            XPointer arg
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - if (event is an Expose event for the window pointed to by arg) then
                    - returns true
                - else
                    - returns false
        !*/</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>event<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>type <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Expose<font face='Lucida Console'>)</font>
            <b>{</b>
                XExposeEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XExposeEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>event<font face='Lucida Console'>)</font>;
                Window<font color='#5555FF'>*</font> win<font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>Window<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>arg<font face='Lucida Console'>)</font>;
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>return</font> <font color='#979000'>1</font>;
                <b>}</b>
            <b>}</b>
            <font color='#0000FF'>return</font> <font color='#979000'>0</font>;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>    
        <font color='#0000FF'>static</font> <font color='#0000FF'><u>bool</u></font> <b><a name='map_keys'></a>map_keys</b> <font face='Lucida Console'>(</font>
            KeySym keycode,
            <font color='#0000FF'><u>bool</u></font> ,
            <font color='#0000FF'><u>bool</u></font> ,
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> result,
            <font color='#0000FF'><u>bool</u></font><font color='#5555FF'>&amp;</font> is_printable
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            requires
                - if (shift was down for this key) then
                    - shift == true
                - if (caps lock was on for this key) then
                    - caps == true
                - keycode == the keycode from windows that we are to process
                - keycode &lt; keyboard_keys_size
            ensures
                - if (this key should be ignored) then
                    - returns false
                - else
                    - returns true
                    - #is_printable == true if result is a printable ascii character
                    - #result == the keycode converted into the proper number to tbe 
                      returned by the event handler.
        !*/</font>
        <b>{</b>
            is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>keycode <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>z</font>' <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> keycode <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>a</font>'<font face='Lucida Console'>)</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> 
                <font face='Lucida Console'>(</font>keycode <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>Z</font>' <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> keycode <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>A</font>'<font face='Lucida Console'>)</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> 
                <font face='Lucida Console'>(</font>keycode <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>9</font>' <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> keycode <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> '<font color='#FF0000'>0</font>'<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                result <font color='#5555FF'>=</font> keycode;
            <b>}</b>
            <font color='#0000FF'>else</font>
            <b>{</b>
                is_printable <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                <font color='#0000FF'>switch</font> <font face='Lucida Console'>(</font>keycode<font face='Lucida Console'>)</font>
                <b>{</b>
                <font color='#0000FF'>case</font> XK_Home:   result <font color='#5555FF'>=</font> base_window::KEY_HOME; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Left:   result <font color='#5555FF'>=</font> base_window::KEY_LEFT; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Right:  result <font color='#5555FF'>=</font> base_window::KEY_RIGHT; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Down:   result <font color='#5555FF'>=</font> base_window::KEY_DOWN; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Up:     result <font color='#5555FF'>=</font> base_window::KEY_UP; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Prior:  result <font color='#5555FF'>=</font> base_window::KEY_PAGE_UP; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Next:   result <font color='#5555FF'>=</font> base_window::KEY_PAGE_DOWN;     <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_End:    result <font color='#5555FF'>=</font> base_window::KEY_END; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Escape:    result <font color='#5555FF'>=</font> base_window::KEY_ESC; <font color='#0000FF'>break</font>;
                
                <font color='#0000FF'>case</font> XK_KP_Delete:    result <font color='#5555FF'>=</font> base_window::KEY_DELETE; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Prior:    result <font color='#5555FF'>=</font> base_window::KEY_PAGE_UP; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Next:    result <font color='#5555FF'>=</font> base_window::KEY_PAGE_DOWN; <font color='#0000FF'>break</font>;


                <font color='#0000FF'>case</font> XK_F1:    result <font color='#5555FF'>=</font> base_window::KEY_F1; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F2:    result <font color='#5555FF'>=</font> base_window::KEY_F2; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F3:    result <font color='#5555FF'>=</font> base_window::KEY_F3; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F4:    result <font color='#5555FF'>=</font> base_window::KEY_F4; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F5:    result <font color='#5555FF'>=</font> base_window::KEY_F5; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F6:    result <font color='#5555FF'>=</font> base_window::KEY_F6; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F7:    result <font color='#5555FF'>=</font> base_window::KEY_F7; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F8:    result <font color='#5555FF'>=</font> base_window::KEY_F8; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F9:    result <font color='#5555FF'>=</font> base_window::KEY_F9; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F10:    result <font color='#5555FF'>=</font> base_window::KEY_F10; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F11:    result <font color='#5555FF'>=</font> base_window::KEY_F11; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_F12:    result <font color='#5555FF'>=</font> base_window::KEY_F12; <font color='#0000FF'>break</font>;
                    
                    
                <font color='#0000FF'>case</font> XK_Shift_L:    result <font color='#5555FF'>=</font> base_window::KEY_SHIFT; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Shift_R:    result <font color='#5555FF'>=</font> base_window::KEY_SHIFT; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Control_L:    result <font color='#5555FF'>=</font> base_window::KEY_CTRL; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Control_R:    result <font color='#5555FF'>=</font> base_window::KEY_CTRL; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Caps_Lock:    result <font color='#5555FF'>=</font> base_window::KEY_CAPS_LOCK; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Alt_L:    result <font color='#5555FF'>=</font> base_window::KEY_ALT; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Alt_R:    result <font color='#5555FF'>=</font> base_window::KEY_ALT; <font color='#0000FF'>break</font>;

                    
                <font color='#0000FF'>case</font> XK_BackSpace:    result <font color='#5555FF'>=</font> base_window::KEY_BACKSPACE; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Delete:    result <font color='#5555FF'>=</font> base_window::KEY_DELETE; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Scroll_Lock:    result <font color='#5555FF'>=</font> base_window::KEY_SCROLL_LOCK; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Pause:    result <font color='#5555FF'>=</font> base_window::KEY_PAUSE; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Insert:    result <font color='#5555FF'>=</font> base_window::KEY_INSERT; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Insert:    result <font color='#5555FF'>=</font> base_window::KEY_INSERT; <font color='#0000FF'>break</font>;




                <font color='#0000FF'>case</font> XK_exclam:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>!</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_quotedbl:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>"</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_numbersign:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>#</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_dollar:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>$</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_percent:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>%</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_ampersand:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>&amp;</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_apostrophe:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>\'</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_parenleft:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>(</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_parenright:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>)</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_asterisk:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>*</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_plus:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>+</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_comma:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>,</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_minus:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>-</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_period:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>.</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_slash:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>/</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_colon:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>:</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_semicolon:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>;</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_less:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>&lt;</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_equal:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>=</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_greater:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>&gt;</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_question:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>?</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_at:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>@</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_grave:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>`</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_underscore:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>_</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_asciicircum:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>^</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_bracketleft:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>[</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_backslash:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>\\</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_bracketright:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>]</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_asciitilde:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>~</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_braceleft:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>{</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_bar:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>|</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_braceright:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>}</font>'; <font color='#0000FF'>break</font>;
            



                <font color='#0000FF'>case</font> XK_space:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'> </font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Return:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>\n</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_Tab:    
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>\t</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Divide: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>/</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Decimal: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>.</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Subtract: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>-</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Add: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>+</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Multiply: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>*</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_Equal: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>=</font>'; <font color='#0000FF'>break</font>;

                <font color='#0000FF'>case</font> XK_KP_0: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>0</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_1: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>1</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_2: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>2</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_3: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>3</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_4: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>4</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_5: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>5</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_6: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>6</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_7: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>7</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_8: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>8</font>'; <font color='#0000FF'>break</font>;
                <font color='#0000FF'>case</font> XK_KP_9: 
                    is_printable <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                    result <font color='#5555FF'>=</font> '<font color='#FF0000'>9</font>'; <font color='#0000FF'>break</font>;

                <font color='#0000FF'>default</font>:
                    <font color='#0000FF'>return</font> <font color='#979000'>false</font>;
                <b>}</b>
            <b>}</b>

            <font color='#0000FF'>return</font> <font color='#979000'>true</font>;
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
        <font color='#0000FF'><u>void</u></font> event_handler_thread::
        <b><a name='event_handler'></a>event_handler</b> <font face='Lucida Console'>(</font>
        <font face='Lucida Console'>)</font>
        <font color='#009900'>/*!
            ensures
                - will handle all events and event dispatching            
        !*/</font>
        <b>{</b>       
            <font color='#0000FF'>try</font>
            <b>{</b>
                std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>&gt;</font> bitmap_buffer;
                <font color='#0000FF'><u>bool</u></font> quit_event_loop <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>quit_event_loop <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#009900'>// get a lock on the window_table's mutex
</font>                    auto_mutex <font color='#BB00BB'>window_table_locker</font><font face='Lucida Console'>(</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

                    XEvent ev;                
                    <font color='#BB00BB'>memset</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev, <font color='#979000'>0</font>, <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>ev<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>XPending</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font><b>{</b>
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <font color='#009900'>// wait until receiving X11 next event
</font>                        <font color='#0000FF'>struct</font> pollfd pfd;
                        pfd.fd <font color='#5555FF'>=</font> <font color='#BB00BB'>ConnectionNumber</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;
                        pfd.events <font color='#5555FF'>=</font> POLLIN <font color='#5555FF'>|</font> POLLPRI;
                        <font color='#BB00BB'>poll</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>pfd, <font color='#979000'>1</font>, <font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;  
                        
                        window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                    <b>}</b>
                    <font color='#BB00BB'>XNextEvent</font><font face='Lucida Console'>(</font>disp,<font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;

                    <font color='#009900'>// pass events to input method.
</font>                    <font color='#009900'>// if this event is needed by input method, XFilterEvent returns True
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>XFilterEvent</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev, None<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> True<font face='Lucida Console'>)</font><b>{</b>
                        <font color='#0000FF'>continue</font>;
                    <b>}</b>

                    <font color='#009900'>// if this event is for one of the windows in the window_table
</font>                    <font color='#009900'>// then get that window out of the table and put it into win.
</font>                    XAnyEvent<font color='#5555FF'>*</font> _ae <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XAnyEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;
                    base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> window_table[_ae<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window];
                    base_window<font color='#5555FF'>*</font> win <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                        win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;


                    <font color='#009900'>// ignore messages for unmapped windows
</font>                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>ev.type <font color='#5555FF'>!</font><font color='#5555FF'>=</font> MapNotify <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> win <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font> 
                    <b>{</b>
                        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>is_mapped <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                           <font color='#0000FF'>continue</font>;
                    <b>}</b>


                    <font color='#0000FF'>switch</font> <font face='Lucida Console'>(</font>ev.type<font face='Lucida Console'>)</font>
                    <b>{</b>

                    <font color='#0000FF'>case</font> SelectionRequest:
                        <b>{</b>
                            Atom a_ct <font color='#5555FF'>=</font> <font color='#BB00BB'>XInternAtom</font><font face='Lucida Console'>(</font>disp, "<font color='#CC0000'>COMPOUND_TEXT</font>", False<font face='Lucida Console'>)</font>;
                            XSelectionRequestEvent<font color='#5555FF'>*</font> req <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XSelectionRequestEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev.xselectionrequest<font face='Lucida Console'>)</font>;
                            XEvent respond;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>target <font color='#5555FF'>=</font><font color='#5555FF'>=</font> XA_STRING<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#BB00BB'>XChangeProperty</font> <font face='Lucida Console'>(</font>disp,
                                                 req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>requestor,
                                                 req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>property,
                                                 XA_STRING,
                                                 <font color='#979000'>8</font>,
                                                 PropModeReplace,
                                                 <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_wstring_to_mbstring</font><font face='Lucida Console'>(</font>clipboard<font face='Lucida Console'>)</font>.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                                                 clipboard.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
                                respond.xselection.property<font color='#5555FF'>=</font>req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>property;
                            <b>}</b>
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>target <font color='#5555FF'>=</font><font color='#5555FF'>=</font> a_ct<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#BB00BB'>XChangeProperty</font> <font face='Lucida Console'>(</font>disp,
                                                 req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>requestor,
                                                 req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>property,
                                                 a_ct,
                                                 <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>wchar_t</u></font><font face='Lucida Console'>)</font><font color='#5555FF'>*</font><font color='#979000'>8</font>,
                                                 PropModeReplace,
                                                 <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>clipboard.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
                                                 clipboard.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>+</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;
                                respond.xselection.property<font color='#5555FF'>=</font>req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>property;
                            <b>}</b>
                            <font color='#0000FF'>else</font> 
                            <b>{</b>
                                respond.xselection.property<font color='#5555FF'>=</font> None;
                            <b>}</b>
                            respond.xselection.type<font color='#5555FF'>=</font> SelectionNotify;
                            respond.xselection.display<font color='#5555FF'>=</font> req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>display;
                            respond.xselection.requestor<font color='#5555FF'>=</font> req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>requestor;
                            respond.xselection.selection<font color='#5555FF'>=</font>req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>selection;
                            respond.xselection.target<font color='#5555FF'>=</font> req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>target;
                            respond.xselection.time <font color='#5555FF'>=</font> req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>time;
                            <font color='#BB00BB'>XSendEvent</font> <font face='Lucida Console'>(</font>disp, req<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>requestor,<font color='#979000'>0</font>,<font color='#979000'>0</font>,<font color='#5555FF'>&amp;</font>respond<font face='Lucida Console'>)</font>;
                            <font color='#BB00BB'>XFlush</font> <font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;

                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> MapNotify:
                        <b>{</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>is_mapped <font color='#5555FF'>=</font> <font color='#979000'>true</font>;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>resizable <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                XSizeHints<font color='#5555FF'>*</font> hints <font color='#5555FF'>=</font> <font color='#BB00BB'>XAllocSizeHints</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>flags <font color='#5555FF'>=</font> PMinSize<font color='#5555FF'>|</font>PMaxSize;
                                hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>min_width <font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width;
                                hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_width <font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width;
                                hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_height <font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height; 
                                hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>min_height <font color='#5555FF'>=</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height; 
                                <font color='#BB00BB'>XSetNormalHints</font><font face='Lucida Console'>(</font>disp,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.hwnd,hints<font face='Lucida Console'>)</font>;
                                <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>hints<font face='Lucida Console'>)</font>;
                            <b>}</b>



                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_resized<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#BB00BB'>XResizeWindow</font><font face='Lucida Console'>(</font>disp,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.hwnd,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height<font face='Lucida Console'>)</font>;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_resized <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_resized</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            <b>}</b>

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_moved<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#BB00BB'>XMoveWindow</font><font face='Lucida Console'>(</font>disp,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.hwnd,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x,win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y<font face='Lucida Console'>)</font>;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_moved <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_moved</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            <b>}</b>
                            <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>disp<font face='Lucida Console'>)</font>;


                        <b>}</b> <font color='#0000FF'>break</font>;


                    <font color='#0000FF'>case</font> KeyPress:
                        <b>{</b>
                            XKeyPressedEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XKeyPressedEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'><u>bool</u></font> shift <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> ShiftMask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'><u>bool</u></font> ctrl <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> ControlMask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'><u>bool</u></font> caps <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> LockMask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>shift<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_SHIFT;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>ctrl<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_CONTROL;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>caps<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_CAPS_LOCK;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> alt_mask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_ALT;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> meta_mask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_META;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> num_lock_mask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_NUM_LOCK;
                            <font color='#0000FF'>if</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> scroll_lock_mask<font face='Lucida Console'>)</font><font color='#5555FF'>!</font><font color='#5555FF'>=</font><font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::KBD_MOD_SCROLL_LOCK;

                            KeySym key;
                            Status status;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.xic<font face='Lucida Console'>)</font> <b>{</b>
                                std::wstring wstr;
                                wstr.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font><font color='#979000'>2</font><font face='Lucida Console'>)</font>;
                                <font color='#0000FF'><u>int</u></font> len <font color='#5555FF'>=</font> <font color='#BB00BB'>XwcLookupString</font><font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.xic,e,<font color='#5555FF'>&amp;</font>wstr[<font color='#979000'>0</font>],wstr.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,<font color='#5555FF'>&amp;</font>key,<font color='#5555FF'>&amp;</font>status<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> XBufferOverflow<font face='Lucida Console'>)</font><b>{</b>
                                    wstr.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>len<font face='Lucida Console'>)</font>;
                                    len <font color='#5555FF'>=</font> <font color='#BB00BB'>XwcLookupString</font><font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.xic,e,<font color='#5555FF'>&amp;</font>wstr[<font color='#979000'>0</font>],wstr.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,<font color='#5555FF'>&amp;</font>key,<font color='#5555FF'>&amp;</font>status<font face='Lucida Console'>)</font>;
                                <b>}</b>
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> XLookupChars<font face='Lucida Console'>)</font><b>{</b>
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_string_put</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
                                <b>}</b>
                            <b>}</b> <font color='#0000FF'>else</font> <b>{</b>
                                <font color='#0000FF'><u>char</u></font> buffer[<font color='#979000'>2</font>];
                                <font color='#BB00BB'>XLookupString</font><font face='Lucida Console'>(</font>e, buffer, <font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>buffer<font face='Lucida Console'>)</font>, <font color='#5555FF'>&amp;</font>key, NULL<font face='Lucida Console'>)</font>;
                                status <font color='#5555FF'>=</font> XLookupKeySym;
                            <b>}</b>

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> XLookupKeySym <font color='#5555FF'>|</font><font color='#5555FF'>|</font> status <font color='#5555FF'>=</font><font color='#5555FF'>=</font> XLookupBoth<font face='Lucida Console'>)</font><b>{</b>

                                <font color='#0000FF'><u>bool</u></font> is_printable;
                                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> result;

                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>map_keys</font><font face='Lucida Console'>(</font>key,shift,caps,result,is_printable<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    <font color='#009900'>// signal the keyboard event
</font>                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_keydown</font><font face='Lucida Console'>(</font>result,is_printable,state<font face='Lucida Console'>)</font>;
                                <b>}</b>
                            <b>}</b>
                            
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> FocusIn:
                        <b>{</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#009900'>// signal the focus event 
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_focus_gained</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> FocusOut:
                        <b>{</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#009900'>// signal the focus event 
</font>                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_focus_lost</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> ButtonPress:
                    <font color='#0000FF'>case</font> ButtonRelease:
                        <b>{</b>
                            XButtonEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XButtonEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> btn <font color='#5555FF'>=</font> base_window::NONE;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>button <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Button1<font face='Lucida Console'>)</font>
                                btn <font color='#5555FF'>=</font> base_window::LEFT;
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>button <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Button3<font face='Lucida Console'>)</font>
                                btn <font color='#5555FF'>=</font> base_window::RIGHT;
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>button <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Button2<font face='Lucida Console'>)</font>
                                btn <font color='#5555FF'>=</font> base_window::MIDDLE;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> ControlMask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::CONTROL;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> Button1Mask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::LEFT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> Button2Mask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::MIDDLE;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> Button3Mask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::RIGHT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> ShiftMask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::SHIFT;

                            <font color='#009900'>// only send the event if this is a button we support
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>btn <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font>base_window::NONE<font face='Lucida Console'>)</font>
                            <b>{</b>


                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>ev.type <font color='#5555FF'>=</font><font color='#5555FF'>=</font> ButtonPress<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    <font color='#0000FF'><u>bool</u></font> is_double_click <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_button <font color='#5555FF'>=</font><font color='#5555FF'>=</font> btn <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                                        std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_x <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font> <font color='#979000'>5</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                                        std::<font color='#BB00BB'>abs</font><font face='Lucida Console'>(</font><font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_y <font color='#5555FF'>-</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font><font face='Lucida Console'>)</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y<font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font> <font color='#979000'>5</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font>
                                        e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>time <font color='#5555FF'>-</font> win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.last_click_time <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> <font color='#979000'>400</font><font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        <font color='#009900'>// this is a double click
</font>                                        is_double_click <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                                        <font color='#009900'>// set this to make sure the next click can't be
</font>                                        <font color='#009900'>// interpreted as a double click
</font>                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_button <font color='#5555FF'>=</font> base_window::NONE;
                                    <b>}</b>
                                    <font color='#0000FF'>else</font>
                                    <b>{</b>
                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_button <font color='#5555FF'>=</font> btn;
                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_x <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x;
                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>last_click_y <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y;
                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x11_stuff.last_click_time <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>time;
                                    <b>}</b>

                                    <font color='#009900'>// remove the clicked button from the state
</font>                                    state <font color='#5555FF'>&amp;</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>~btn<font face='Lucida Console'>)</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_down</font><font face='Lucida Console'>(</font>btn,state,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y,is_double_click<font face='Lucida Console'>)</font>;

                                <b>}</b>
                                <font color='#0000FF'>else</font>
                                <b>{</b>
                                    <font color='#009900'>// remove the clicked button from the state
</font>                                    state <font color='#5555FF'>&amp;</font><font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>~btn<font face='Lucida Console'>)</font>;
                                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_up</font><font face='Lucida Console'>(</font>btn,state,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y<font face='Lucida Console'>)</font>;
                                <b>}</b>
                            <b>}</b>
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>button <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Button4 <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> ev.type <font color='#5555FF'>=</font><font color='#5555FF'>=</font> ButtonPress<font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_wheel_up</font><font face='Lucida Console'>(</font>state<font face='Lucida Console'>)</font>;
                            <b>}</b>
                            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>button <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Button5 <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> ev.type <font color='#5555FF'>=</font><font color='#5555FF'>=</font> ButtonPress<font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_wheel_down</font><font face='Lucida Console'>(</font>state<font face='Lucida Console'>)</font>;
                            <b>}</b>
                            
                        <b>}</b> <font color='#0000FF'>break</font>;
 
                    <font color='#0000FF'>case</font> LeaveNotify:
                        <b>{</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_leave</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> EnterNotify:
                        <b>{</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_enter</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> MotionNotify:
                        <b>{</b>
                            XMotionEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XMotionEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> state <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> ControlMask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::CONTROL;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> Button1Mask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::LEFT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> Button2Mask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::MIDDLE;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> Button3Mask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::RIGHT;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>state <font color='#5555FF'>&amp;</font> ShiftMask<font face='Lucida Console'>)</font>
                                state <font color='#5555FF'>|</font><font color='#5555FF'>=</font> base_window::SHIFT;

                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_mouse_move</font><font face='Lucida Console'>(</font>state,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y<font face='Lucida Console'>)</font>;
                            
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> ConfigureNotify:
                        <b>{</b>
                            XConfigureEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XConfigureEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window <font color='#5555FF'>=</font><font color='#5555FF'>=</font> exit_window<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// this is the signal to quit the event handler
</font>                                quit_event_loop <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                                <font color='#0000FF'>break</font>;
                            <b>}</b>

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width <font color='#5555FF'>!</font><font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width <font color='#5555FF'>|</font><font color='#5555FF'>|</font>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height <font color='#5555FF'>!</font><font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height <font color='#5555FF'>|</font><font color='#5555FF'>|</font>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_resized<font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_resized <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                <font color='#009900'>// this is a resize
</font>                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_resized</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            <b>}</b>
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x <font color='#5555FF'>!</font><font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x <font color='#5555FF'>|</font><font color='#5555FF'>|</font>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y <font color='#5555FF'>!</font><font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y <font color='#5555FF'>|</font><font color='#5555FF'>|</font>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_moved<font face='Lucida Console'>)</font>
                            <b>{</b>
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_moved <font color='#5555FF'>=</font> <font color='#979000'>false</font>;
                                <font color='#009900'>// this is a move
</font>                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y;
                                win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_moved</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                            <b>}</b>
                            
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> ClientMessage:
                        <b>{</b>
                            XClientMessageEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XClientMessageEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;
                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font face='Lucida Console'>(</font>Atom<font face='Lucida Console'>)</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>data.l[<font color='#979000'>0</font>] <font color='#5555FF'>=</font><font color='#5555FF'>=</font> delete_window<font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                    <font color='#0000FF'>break</font>;


                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_window_close</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> base_window::DO_NOT_CLOSE_WINDOW<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font>,
                                        "<font color='#CC0000'>\tYou called close_window() inside the on_window_close() event but</font>" 
                                        <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthen returned DO_NOT_CLOSE_WINDOW.  You can do one or the other but not both.</font>"
                                        <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> win 
                                        <font face='Lucida Console'>)</font>;
                                    <font color='#009900'>// the client has decided not to close the window
</font>                                    <font color='#009900'>// after all
</font>                                <b>}</b>
                                <font color='#0000FF'>else</font>
                                <b>{</b>                                
                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>window_table[e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window]<font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        window_table.<font color='#BB00BB'>destroy</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window<font face='Lucida Console'>)</font>;
                                        <font color='#BB00BB'>XDestroyWindow</font><font face='Lucida Console'>(</font>disp,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window<font face='Lucida Console'>)</font>;
                                        win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_destroyed <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
                                        window_close_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                                    <b>}</b>
                                    <font color='#0000FF'>else</font>
                                    <b>{</b>
                                        <font color='#009900'>// in this case the window must have self destructed by
</font>                                        <font color='#009900'>// calling delete this;  so we don't have to do anything.
</font>                                    <b>}</b>
                                <b>}</b>
                            <b>}</b>
                        <b>}</b> <font color='#0000FF'>break</font>;

                    <font color='#0000FF'>case</font> Expose:
                        <b>{</b>
                            XExposeEvent<font color='#5555FF'>*</font> e <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XExposeEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>ev<font face='Lucida Console'>)</font>;

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#009900'>// take all the expose events for this window out
</font>                            XEvent etemp;
                            <font color='#0000FF'><u>int</u></font> x <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x;
                            <font color='#0000FF'><u>int</u></font> y <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y;
                            <font color='#0000FF'><u>int</u></font> width <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width;
                            <font color='#0000FF'><u>int</u></font> height <font color='#5555FF'>=</font> e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height;  



                            <font color='#009900'>// What we are doing here with this loop is we are combining
</font>                            <font color='#009900'>// all of the Expose events for this window that are 
</font>                            <font color='#009900'>// currently in the queue.  
</font>                            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font><font color='#BB00BB'>XCheckIfEvent</font><font face='Lucida Console'>(</font>disp,<font color='#5555FF'>&amp;</font>etemp,XCheckIfEventPredicate,<font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XPointer<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font><font face='Lucida Console'>(</font>e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                XExposeEvent<font color='#5555FF'>*</font> e2 <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>XExposeEvent<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>etemp<font face='Lucida Console'>)</font>;
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x <font color='#5555FF'>&lt;</font> x<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    width <font color='#5555FF'>+</font><font color='#5555FF'>=</font> x <font color='#5555FF'>-</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x;
                                    x <font color='#5555FF'>=</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x;                                
                                <b>}</b>
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y <font color='#5555FF'>&lt;</font> y<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    height <font color='#5555FF'>+</font><font color='#5555FF'>=</font> y <font color='#5555FF'>-</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y;
                                    y <font color='#5555FF'>=</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y;
                                <b>}</b>
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width <font color='#5555FF'>+</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x <font color='#5555FF'>&gt;</font> width <font color='#5555FF'>+</font> x<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    width <font color='#5555FF'>=</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>width <font color='#5555FF'>+</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>x <font color='#5555FF'>-</font> x;
                                <b>}</b>
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height <font color='#5555FF'>+</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y <font color='#5555FF'>&gt;</font> height <font color='#5555FF'>+</font> y<font face='Lucida Console'>)</font>
                                <b>{</b>
                                    height <font color='#5555FF'>=</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>height <font color='#5555FF'>+</font> e2<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>y <font color='#5555FF'>-</font> y;
                                <b>}</b>                                
                            <b>}</b>

                            <font color='#009900'>// I'm not sure if this sort of thing can happen but
</font>                            <font color='#009900'>// if it does then just ignore this entire event.
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>width <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font color='#5555FF'>|</font><font color='#5555FF'>|</font> height <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#0000FF'>break</font>;
                            <b>}</b>

                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>bitmap_buffer.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>width<font color='#5555FF'>*</font>height<font color='#5555FF'>*</font><font color='#979000'>4</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                                bitmap_buffer.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>width<font color='#5555FF'>*</font>height<font color='#5555FF'>*</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> bitmap <font color='#5555FF'>=</font> <font color='#5555FF'>&amp;</font>bitmap_buffer[<font color='#979000'>0</font>];
                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> <font color='#0000FF'>const</font> end <font color='#5555FF'>=</font> bitmap <font color='#5555FF'>+</font> width<font color='#5555FF'>*</font>height<font color='#5555FF'>*</font><font color='#979000'>4</font>;

                            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font> temp;
                            canvas <font color='#BB00BB'>c</font><font face='Lucida Console'>(</font>bitmap,x,y,x<font color='#5555FF'>+</font>width<font color='#5555FF'>-</font><font color='#979000'>1</font>,y<font color='#5555FF'>+</font>height<font color='#5555FF'>-</font><font color='#979000'>1</font><font face='Lucida Console'>)</font>;


                            win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>paint</font><font face='Lucida Console'>(</font>c<font face='Lucida Console'>)</font>;

                            <font color='#009900'>// the user might have called win-&gt;close_window() and if they did
</font>                            <font color='#009900'>// then just stop right here.  We don't want to paint the window.
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>has_been_destroyed<font face='Lucida Console'>)</font>
                                <font color='#0000FF'>break</font>;

                            <font color='#009900'>// if the color depth we are working with isn't 24bits then we need
</font>                            <font color='#009900'>// to transform our image into whatever it is supposed to be.
</font>                            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>depth <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>24</font><font face='Lucida Console'>)</font>
                            <b>{</b>
                                <font color='#009900'>// convert this image into an 8 bit image
</font>                                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> red_bits <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> green_bits <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> blue_bits <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>depth <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>16</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> bits <font color='#5555FF'>=</font> depth<font color='#5555FF'>/</font><font color='#979000'>3</font>;
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>int</u></font> extra <font color='#5555FF'>=</font> depth<font color='#5555FF'>%</font><font color='#979000'>3</font>;
                                    red_bits <font color='#5555FF'>=</font> bits;
                                    green_bits <font color='#5555FF'>=</font> bits;
                                    blue_bits <font color='#5555FF'>=</font> bits;
                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>extra<font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>red_bits;
                                        <font color='#5555FF'>-</font><font color='#5555FF'>-</font>extra;
                                    <b>}</b>
                                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>extra<font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>green_bits;
                                    <b>}</b>
                                <b>}</b>
                                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>depth <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>16</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    red_bits <font color='#5555FF'>=</font> <font color='#979000'>5</font>;
                                    green_bits <font color='#5555FF'>=</font> <font color='#979000'>6</font>;
                                    blue_bits <font color='#5555FF'>=</font> <font color='#979000'>5</font>;
                                <b>}</b>

                                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>depth <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>16</font><font face='Lucida Console'>)</font> 
                                <b>{</b> 
                                    temp <font color='#5555FF'>=</font> bitmap;
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font>red, <font color='#5555FF'>*</font>green, <font color='#5555FF'>*</font>blue;
                                    <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>!</font><font color='#5555FF'>=</font> end<font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        blue <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        green <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        red <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;

                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>red<font face='Lucida Console'>)</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>8</font><font color='#5555FF'>-</font>red_bits<font face='Lucida Console'>)</font>;
                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> g <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>green<font face='Lucida Console'>)</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>8</font><font color='#5555FF'>-</font>green_bits<font face='Lucida Console'>)</font>;
                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> b <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>blue<font face='Lucida Console'>)</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>8</font><font color='#5555FF'>-</font>blue_bits<font face='Lucida Console'>)</font>;

                                        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> color <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>r<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>-</font>red_bits<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>g<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>-</font>red_bits<font color='#5555FF'>-</font>green_bits<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>|</font> b;

                                        <font color='#5555FF'>*</font>blue  <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                        <font color='#5555FF'>*</font>green <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                    <b>}</b>
                                <b>}</b>
                                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>depth <font color='#5555FF'>&lt;</font> <font color='#979000'>24</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    temp <font color='#5555FF'>=</font> bitmap;
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font>red, <font color='#5555FF'>*</font>green, <font color='#5555FF'>*</font>blue;
                                    <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>!</font><font color='#5555FF'>=</font> end<font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        blue <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        green <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        red <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;

                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>red<font face='Lucida Console'>)</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>8</font><font color='#5555FF'>-</font>red_bits<font face='Lucida Console'>)</font>;
                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> g <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>green<font face='Lucida Console'>)</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>8</font><font color='#5555FF'>-</font>green_bits<font face='Lucida Console'>)</font>;
                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> b <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>blue<font face='Lucida Console'>)</font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#979000'>8</font><font color='#5555FF'>-</font>blue_bits<font face='Lucida Console'>)</font>;

                                        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> color <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>b<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>-</font>blue_bits<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>g<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>-</font>blue_bits<font color='#5555FF'>-</font>green_bits<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>|</font> r;

                                        <font color='#5555FF'>*</font>blue  <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                        <font color='#5555FF'>*</font>green <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                        <font color='#5555FF'>*</font>red   <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>16</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                    <b>}</b>
                                <b>}</b>
                                <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>depth <font color='#5555FF'>&gt;</font> <font color='#979000'>24</font><font face='Lucida Console'>)</font>
                                <b>{</b>
                                    temp <font color='#5555FF'>=</font> bitmap;
                                    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font>red, <font color='#5555FF'>*</font>green, <font color='#5555FF'>*</font>blue, <font color='#5555FF'>*</font>four;
                                    <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>temp <font color='#5555FF'>!</font><font color='#5555FF'>=</font> end<font face='Lucida Console'>)</font>
                                    <b>{</b>
                                        blue <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        green <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        red <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;
                                        four <font color='#5555FF'>=</font> temp;
                                        <font color='#5555FF'>+</font><font color='#5555FF'>+</font>temp;

                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>red<font face='Lucida Console'>)</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>red_bits<font color='#5555FF'>-</font><font color='#979000'>8</font><font face='Lucida Console'>)</font>;
                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> g <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>green<font face='Lucida Console'>)</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>green_bits<font color='#5555FF'>-</font><font color='#979000'>8</font><font face='Lucida Console'>)</font>;
                                        <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> b <font color='#5555FF'>=</font> <font color='#0000FF'>static_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font color='#5555FF'>*</font>blue<font face='Lucida Console'>)</font><font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>blue_bits<font color='#5555FF'>-</font><font color='#979000'>8</font><font face='Lucida Console'>)</font>;

                                        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> color <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>b<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>-</font>blue_bits<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>|</font> <font face='Lucida Console'>(</font>g<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font>depth<font color='#5555FF'>-</font>blue_bits<font color='#5555FF'>-</font>green_bits<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font color='#5555FF'>|</font> r;

                                        <font color='#5555FF'>*</font>blue  <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>0</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                        <font color='#5555FF'>*</font>green <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>8</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                        <font color='#5555FF'>*</font>red   <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>16</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                        <font color='#5555FF'>*</font>four  <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>color<font color='#5555FF'>&gt;</font><font color='#5555FF'>&gt;</font><font color='#979000'>24</font><font face='Lucida Console'>)</font><font color='#5555FF'>&amp;</font><font color='#979000'>0xFF</font>;
                                    <b>}</b>
                                <b>}</b>
                            <b>}</b> <font color='#009900'>// if (depth != 24)
</font>


                            XImage img;
                            <font color='#BB00BB'>memset</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>img,<font color='#979000'>0</font>,<font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>img<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                            img.width <font color='#5555FF'>=</font> width;
                            img.height <font color='#5555FF'>=</font> height;
                            img.depth <font color='#5555FF'>=</font> depth;
                            img.data <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>char</u></font><font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>bitmap<font face='Lucida Console'>)</font>;
                            img.bitmap_bit_order <font color='#5555FF'>=</font> LSBFirst;
                            img.byte_order <font color='#5555FF'>=</font> LSBFirst;
                            img.format <font color='#5555FF'>=</font> ZPixmap;
                            img.bitmap_pad <font color='#5555FF'>=</font> <font color='#979000'>32</font>;
                            img.bitmap_unit <font color='#5555FF'>=</font> <font color='#979000'>32</font>;
                            img.bits_per_pixel <font color='#5555FF'>=</font> <font color='#979000'>32</font>;


                            <font color='#BB00BB'>XInitImage</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>img<font face='Lucida Console'>)</font>;

                            GC gc <font color='#5555FF'>=</font> <font color='#BB00BB'>XCreateGC</font><font face='Lucida Console'>(</font>disp, e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window, <font color='#979000'>0</font>, NULL<font face='Lucida Console'>)</font>;

                            <font color='#BB00BB'>XPutImage</font><font face='Lucida Console'>(</font>disp,e<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window,gc,<font color='#5555FF'>&amp;</font>img,<font color='#979000'>0</font>,<font color='#979000'>0</font>,x,y,width,height<font face='Lucida Console'>)</font>;

                            <font color='#BB00BB'>XFreeGC</font><font face='Lucida Console'>(</font>disp,gc<font face='Lucida Console'>)</font>;
                        <b>}</b> <font color='#0000FF'>break</font>;
                    <b>}</b> <font color='#009900'>// switch (ev.type)
</font>                <b>}</b>
            <b>}</b>
            <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>std::exception<font color='#5555FF'>&amp;</font> e<font face='Lucida Console'>)</font>
            <b>{</b>
                dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Exception thrown in event handler: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <b>}</b>
            <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
            <b>{</b>
                dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LFATAL <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Unknown exception thrown in event handler.</font>";
            <b>}</b>
        <b>}</b>
 
    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>

        <font color='#0000FF'><u>int</u></font> <b><a name='index_to_modmask'></a>index_to_modmask</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> n<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>switch</font> <font face='Lucida Console'>(</font> n <font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>case</font> <font color='#979000'>0</font>:
                    <font color='#0000FF'>return</font> Mod1Mask;
                <font color='#0000FF'>case</font> <font color='#979000'>1</font>:
                    <font color='#0000FF'>return</font> Mod2Mask;
                <font color='#0000FF'>case</font> <font color='#979000'>2</font>:
                    <font color='#0000FF'>return</font> Mod3Mask;
                <font color='#0000FF'>case</font> <font color='#979000'>3</font>:
                    <font color='#0000FF'>return</font> Mod4Mask;
            <b>}</b>
            <font color='#0000FF'>return</font> Mod5Mask;
        <b>}</b>

        <font color='#0000FF'><u>void</u></font> event_handler_thread::
        <b><a name='init_keyboard_mod_masks'></a>init_keyboard_mod_masks</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
        <b>{</b>
            XModifierKeymap<font color='#5555FF'>*</font> map <font color='#5555FF'>=</font> <font color='#BB00BB'>XGetModifierMapping</font><font face='Lucida Console'>(</font> disp <font face='Lucida Console'>)</font>;
            KeyCode<font color='#5555FF'>*</font> codes <font color='#5555FF'>=</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>modifiermap <font color='#5555FF'>+</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod <font color='#5555FF'>*</font> Mod1MapIndex;
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> n <font color='#5555FF'>=</font> <font color='#979000'>0</font>; n <font color='#5555FF'>&lt;</font> <font color='#979000'>5</font> <font color='#5555FF'>*</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod; n<font color='#5555FF'>+</font><font color='#5555FF'>+</font> <font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> codes[n] <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>
                    <font color='#0000FF'>continue</font>;
                <font color='#0000FF'>switch</font><font face='Lucida Console'>(</font><font color='#BB00BB'>XkbKeycodeToKeysym</font><font face='Lucida Console'>(</font> disp, codes[n], <font color='#979000'>0</font>, <font color='#979000'>0</font> <font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#0000FF'>case</font> XK_Alt_L:
                        alt_mask <font color='#5555FF'>=</font> <font color='#BB00BB'>index_to_modmask</font><font face='Lucida Console'>(</font>n <font color='#5555FF'>/</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod<font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>continue</font>;
                    <font color='#0000FF'>case</font> XK_Alt_R:
                        <font color='#0000FF'>if</font><font face='Lucida Console'>(</font>alt_mask <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
                            alt_mask <font color='#5555FF'>=</font> <font color='#BB00BB'>index_to_modmask</font><font face='Lucida Console'>(</font>n <font color='#5555FF'>/</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod<font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>continue</font>;
                    <font color='#0000FF'>case</font> XK_Meta_L:
                    <font color='#0000FF'>case</font> XK_Meta_R:
                        meta_mask <font color='#5555FF'>=</font> <font color='#BB00BB'>index_to_modmask</font><font face='Lucida Console'>(</font>n <font color='#5555FF'>/</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod<font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>continue</font>;
                    <font color='#0000FF'>case</font> XK_Scroll_Lock:
                        scroll_lock_mask <font color='#5555FF'>=</font> <font color='#BB00BB'>index_to_modmask</font><font face='Lucida Console'>(</font>n <font color='#5555FF'>/</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod<font face='Lucida Console'>)</font>;
                        <font color='#0000FF'>continue</font>;
                    <font color='#0000FF'>case</font> XK_Num_Lock:
                        num_lock_mask <font color='#5555FF'>=</font> <font color='#BB00BB'>index_to_modmask</font><font face='Lucida Console'>(</font>n <font color='#5555FF'>/</font> map<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_keypermod<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>default</font>:
                        <font color='#0000FF'>continue</font>;
                <b>}</b>
            <b>}</b>
            <font color='#BB00BB'>XFreeModifiermap</font><font face='Lucida Console'>(</font> map <font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> alt_mask <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>
            <b>{</b>
                dlog <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> LWARN <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Search for Alt-key faild.</font>";
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> meta_mask <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font> <font face='Lucida Console'>)</font>
                    alt_mask <font color='#5555FF'>=</font> meta_mask;
                <font color='#0000FF'>else</font>
                    alt_mask <font color='#5555FF'>=</font> Mod1Mask; <font color='#009900'>// resort to guessing
</font>            <b>}</b>
        <b>}</b>

    <font color='#009900'>// ----------------------------------------------------------------------------------------
</font>




    <b>}</b> <font color='#009900'>// namespace gui_core_kernel_2_globals
</font>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> canvas::
    <b><a name='fill'></a>fill</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> red_,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> green_,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> blue_
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        pixel pixel_value;
        pixel_value.red <font color='#5555FF'>=</font> red_;
        pixel_value.green <font color='#5555FF'>=</font> green_;
        pixel_value.blue <font color='#5555FF'>=</font> blue_;
        pixel_value._padding <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

        pixel<font color='#5555FF'>*</font> start <font color='#5555FF'>=</font> <font color='#0000FF'>reinterpret_cast</font><font color='#5555FF'>&lt;</font>pixel<font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>bits<font face='Lucida Console'>)</font>;
        pixel<font color='#5555FF'>*</font> end <font color='#5555FF'>=</font> start <font color='#5555FF'>+</font> width_<font color='#5555FF'>*</font>height_;

        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>start <font color='#5555FF'>!</font><font color='#5555FF'>=</font> end<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#5555FF'>*</font>start <font color='#5555FF'>=</font> pixel_value;
            <font color='#5555FF'>+</font><font color='#5555FF'>+</font>start;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> <b><a name='put_on_clipboard'></a>put_on_clipboard</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>put_on_clipboard</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>str<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='put_on_clipboard'></a>put_on_clipboard</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> dlib::ustring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>put_on_clipboard</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_utf32_to_wstring</font><font face='Lucida Console'>(</font>str<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='put_on_clipboard'></a>put_on_clipboard</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::wstring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;

        shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>clipboard <font color='#5555FF'>=</font> str.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

        <font color='#BB00BB'>XSetSelectionOwner</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,XA_PRIMARY,globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>exit_window,CurrentTime<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    Bool <b><a name='clip_peek_helper'></a>clip_peek_helper</b> <font face='Lucida Console'>(</font>
        Display<font color='#5555FF'>*</font>,
        XEvent<font color='#5555FF'>*</font> event,
        XPointer 
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font> event<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>type <font color='#5555FF'>=</font><font color='#5555FF'>=</font> SelectionNotify<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>return</font> True;
        <b>}</b>
        <font color='#0000FF'>else</font>
        <b>{</b>
            <font color='#0000FF'>return</font> False;
        <b>}</b>
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='get_from_clipboard'></a>get_from_clipboard</b> <font face='Lucida Console'>(</font>
        std::string<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        std::wstring wstr;
        <font color='#BB00BB'>get_from_clipboard</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
        str <font color='#5555FF'>=</font> <font color='#BB00BB'>convert_wstring_to_mbstring</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='get_from_clipboard'></a>get_from_clipboard</b> <font face='Lucida Console'>(</font>
        dlib::ustring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        std::wstring wstr;
        <font color='#BB00BB'>get_from_clipboard</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
        str <font color='#5555FF'>=</font> <font color='#BB00BB'>convert_wstring_to_utf32</font><font face='Lucida Console'>(</font>wstr<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='get_from_clipboard'></a>get_from_clipboard</b> <font face='Lucida Console'>(</font>
        std::wstring<font color='#5555FF'>&amp;</font> str
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        str.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font>data <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'><u>wchar_t</u></font> <font color='#5555FF'>*</font><font color='#5555FF'>*</font>plist <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        Window sown;
        Atom  type;
        <font color='#0000FF'><u>int</u></font> format, result;
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> len, bytes_left, dummy;
        XEvent e;

        <font color='#0000FF'>try</font>
        <b>{</b>
            Atom atom_ct <font color='#5555FF'>=</font> <font color='#BB00BB'>XInternAtom</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, "<font color='#CC0000'>COMPOUND_TEXT</font>", False<font face='Lucida Console'>)</font>;
            sown <font color='#5555FF'>=</font> <font color='#BB00BB'>XGetSelectionOwner</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, XA_PRIMARY<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>sown <font color='#5555FF'>=</font><font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>exit_window<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// if we are copying from ourselfs then don't fool with the Xwindows junk.
</font>                str <font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>clipboard.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <b>}</b>
            <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>sown <font color='#5555FF'>!</font><font color='#5555FF'>=</font> None<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// request that the selection be copied into the XA_PRIMARY property
</font>                <font color='#009900'>// of the exit_window.  It doesn't matter what window we put it in 
</font>                <font color='#009900'>// so long as it is one under the control of this process and exit_window
</font>                <font color='#009900'>// is easy to use here so that is what I'm using.
</font>                <font color='#BB00BB'>XConvertSelection</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, XA_PRIMARY, atom_ct, XA_PRIMARY,
                                   globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>exit_window, CurrentTime<font face='Lucida Console'>)</font>;

                <font color='#009900'>// This will wait until we get a SelectionNotify event which should happen
</font>                <font color='#009900'>// really soon.
</font>                <font color='#BB00BB'>XPeekIfEvent</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,<font color='#5555FF'>&amp;</font>e,clip_peek_helper,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;

                <font color='#009900'>// See how much data we got
</font>                <font color='#BB00BB'>XGetWindowProperty</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>exit_window, 
                                    XA_PRIMARY,    <font color='#009900'>// Tricky..
</font>                                    <font color='#979000'>0</font>, <font color='#979000'>0</font>,         <font color='#009900'>// offset - len
</font>                                    <font color='#979000'>0</font>,        <font color='#009900'>// Delete 0==FALSE
</font>                                    AnyPropertyType,  <font color='#009900'>//flag
</font>                                    <font color='#5555FF'>&amp;</font>type,        <font color='#009900'>// return type
</font>                                    <font color='#5555FF'>&amp;</font>format,      <font color='#009900'>// return format
</font>                                    <font color='#5555FF'>&amp;</font>len, <font color='#5555FF'>&amp;</font>bytes_left,  <font color='#009900'>//that 
</font>                                    <font color='#5555FF'>&amp;</font>data<font face='Lucida Console'>)</font>;             
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font>
                <b>{</b>
                    <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font>;
                    data <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                <b>}</b>
                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>bytes_left <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> type <font color='#5555FF'>=</font><font color='#5555FF'>=</font> atom_ct<font face='Lucida Console'>)</font>
                <b>{</b>
                    XTextProperty p;
                    result <font color='#5555FF'>=</font> <font color='#BB00BB'>XGetWindowProperty</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>exit_window, 
                                                 XA_PRIMARY, <font color='#979000'>0</font>,bytes_left,<font color='#979000'>0</font>,
                                                 AnyPropertyType, <font color='#5555FF'>&amp;</font>p.encoding,<font color='#5555FF'>&amp;</font>p.format,
                                                 <font color='#5555FF'>&amp;</font>p.nitems, <font color='#5555FF'>&amp;</font>dummy, <font color='#5555FF'>&amp;</font>p.value<font face='Lucida Console'>)</font>;
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>result <font color='#5555FF'>=</font><font color='#5555FF'>=</font> Success <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> p.encoding <font color='#5555FF'>=</font><font color='#5555FF'>=</font> atom_ct<font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#0000FF'><u>int</u></font> n;
                        <font color='#BB00BB'>XwcTextPropertyToTextList</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, <font color='#5555FF'>&amp;</font>p, <font color='#5555FF'>&amp;</font>plist, <font color='#5555FF'>&amp;</font>n<font face='Lucida Console'>)</font>;
                        str <font color='#5555FF'>=</font> plist[<font color='#979000'>0</font>];
                    <b>}</b>
                    <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>plist<font face='Lucida Console'>)</font>
                    <b>{</b>
                        <font color='#BB00BB'>XwcFreeStringList</font><font face='Lucida Console'>(</font>plist<font face='Lucida Console'>)</font>;
                        plist <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
                    <b>}</b>
                <b>}</b>
            <b>}</b>
        <b>}</b>
        <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>...<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font> 
                <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>data<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>plist<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#BB00BB'>XwcFreeStringList</font><font face='Lucida Console'>(</font>plist<font face='Lucida Console'>)</font>;
                plist <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            <b>}</b>
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals
    <b>{</b>
        <font color='#0000FF'><u>void</u></font> <b><a name='trigger_user_event_threadproc'></a>trigger_user_event_threadproc</b> <font face='Lucida Console'>(</font>
            <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font>
        <font face='Lucida Console'>)</font>
        <b>{</b>
            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>lock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp<font face='Lucida Console'>)</font>;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>unlock</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;


            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>reset</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#009900'>// now dispatch all these user events
</font>            <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>move_next</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                base_window<font color='#5555FF'>*</font><font color='#5555FF'>*</font> win_ <font color='#5555FF'>=</font> globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table[globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.w];
                base_window<font color='#5555FF'>*</font> win;
                <font color='#009900'>// if this window exists in the window table then dispatch
</font>                <font color='#009900'>// its event.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>win_<font face='Lucida Console'>)</font>
                <b>{</b>
                    win <font color='#5555FF'>=</font> <font color='#5555FF'>*</font>win_;
                    win<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font><font color='#BB00BB'>on_user_event</font><font face='Lucida Console'>(</font>
                        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.p,
                        globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>element</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.i
                    <font face='Lucida Console'>)</font>;
                <b>}</b>
            <b>}</b>
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events_temp.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='trigger_user_event'></a>trigger_user_event</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font> p,
        <font color='#0000FF'><u>int</u></font> i
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        user_event_type e;
        e.w <font color='#5555FF'>=</font> x11_stuff.hwnd;
        e.p <font color='#5555FF'>=</font> p;
        e.i <font color='#5555FF'>=</font> i;
        <b>{</b>
            shared_ptr_thread_safe<font color='#5555FF'>&lt;</font>event_handler_thread<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>globals</font><font face='Lucida Console'>(</font><font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>get_mutex</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>e<font face='Lucida Console'>)</font>;

            <font color='#009900'>// we only need to start a thread to deal with this if there isn't already
</font>            <font color='#009900'>// one out working on the queue
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>user_events.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
                <font color='#BB00BB'>create_new_thread</font> <font face='Lucida Console'>(</font>trigger_user_event_threadproc,<font color='#979000'>0</font><font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    base_window::
    <b><a name='base_window'></a>base_window</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>bool</u></font> resizable_,
        <font color='#0000FF'><u>bool</u></font> undecorated
    <font face='Lucida Console'>)</font> :
        x11_stuff<font face='Lucida Console'>(</font><font color='#5555FF'>*</font><font face='Lucida Console'>(</font><font color='#0000FF'>new</font> gui_core_kernel_2_globals::x11_base_windowstuff<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>,
        is_mapped<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
        resizable<font face='Lucida Console'>(</font>resizable_<font face='Lucida Console'>)</font>,
        has_been_destroyed<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
        has_been_resized<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
        has_been_moved<font face='Lucida Console'>(</font><font color='#979000'>false</font><font face='Lucida Console'>)</font>,
        wm<font face='Lucida Console'>(</font>gui_core_kernel_2_globals::global_data<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.get_mutex<font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font><font color='#5555FF'>!</font><font face='Lucida Console'>(</font>undecorated <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> resizable_ <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>,
            "<font color='#CC0000'>\tbase_window::base_window()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tThere is no such thing as an undecorated window that is resizable by the user.</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\tthis:     </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font color='#0000FF'>this</font>
            <font face='Lucida Console'>)</font>;
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;

        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;

        x11_stuff.globals <font color='#5555FF'>=</font> <font color='#BB00BB'>global_data</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        
        x11_stuff.last_click_time <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        last_click_x <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        last_click_y <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        last_click_button <font color='#5555FF'>=</font> NONE;

        XSetWindowAttributes attr;
        <font color='#BB00BB'>memset</font><font face='Lucida Console'>(</font><font color='#5555FF'>&amp;</font>attr,'<font color='#FF0000'>\0</font>',<font color='#0000FF'>sizeof</font><font face='Lucida Console'>(</font>attr<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> valuemask <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>undecorated<font face='Lucida Console'>)</font>
        <b>{</b>
            attr.override_redirect <font color='#5555FF'>=</font> True;
            valuemask <font color='#5555FF'>=</font> CWOverrideRedirect;
        <b>}</b>


        x11_stuff.hwnd <font color='#5555FF'>=</font> <font color='#BB00BB'>XCreateWindow</font><font face='Lucida Console'>(</font>
                        x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,
                        <font color='#BB00BB'>DefaultRootWindow</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>,
                        <font color='#979000'>0</font>,
                        <font color='#979000'>0</font>,
                        <font color='#979000'>10</font>,  <font color='#009900'>// this is the default width of a window
</font>                        <font color='#979000'>10</font>,  <font color='#009900'>// this is the default width of a window
</font>                        <font color='#979000'>0</font>,
                        x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>depth,
                        InputOutput,
                        CopyFromParent,
                        valuemask,
                        <font color='#5555FF'>&amp;</font>attr
                        <font face='Lucida Console'>)</font>;

        x11_stuff.xic <font color='#5555FF'>=</font> NULL;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>xim<font face='Lucida Console'>)</font>
        <b>{</b>
            XVaNestedList   xva_nlist;
            XPoint          xpoint;

            <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font><font color='#5555FF'>*</font>mlist;
            <font color='#0000FF'><u>int</u></font> mcount;
            <font color='#0000FF'><u>char</u></font> <font color='#5555FF'>*</font>def_str;
            <font color='#0000FF'><u>char</u></font> fontset[<font color='#979000'>256</font>];
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> native_font_height <font color='#5555FF'>=</font> <font color='#979000'>12</font>;
            <font color='#BB00BB'>sprintf</font><font face='Lucida Console'>(</font>fontset, "<font color='#CC0000'>-*-*-medium-r-normal--%lu-*-*-*-</font>", native_font_height<font face='Lucida Console'>)</font>;
            x11_stuff.fs <font color='#5555FF'>=</font> <font color='#BB00BB'>XCreateFontSet</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, fontset, <font color='#5555FF'>&amp;</font>mlist, <font color='#5555FF'>&amp;</font>mcount, <font color='#5555FF'>&amp;</font>def_str<font face='Lucida Console'>)</font>;
            xpoint.x <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            xpoint.y <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            xva_nlist <font color='#5555FF'>=</font> <font color='#BB00BB'>XVaCreateNestedList</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>, XNSpotLocation, <font color='#5555FF'>&amp;</font>xpoint, XNFontSet, x11_stuff.fs, <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>NULL<font face='Lucida Console'>)</font>;
            x11_stuff.xic <font color='#5555FF'>=</font> <font color='#BB00BB'>XCreateIC</font><font face='Lucida Console'>(</font>
                x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>xim,
                XNInputStyle, x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>xim_style,
                XNClientWindow, x11_stuff.hwnd,
                XNPreeditAttributes, xva_nlist,
                <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>NULL
                <font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>xva_nlist<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>XFreeStringList</font><font face='Lucida Console'>(</font>mlist<font face='Lucida Console'>)</font>;
        <b>}</b>

        Window temp <font color='#5555FF'>=</font> x11_stuff.hwnd;
        base_window<font color='#5555FF'>*</font> ttemp <font color='#5555FF'>=</font> <font color='#0000FF'>this</font>;
        x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>add</font><font face='Lucida Console'>(</font>temp,ttemp<font face='Lucida Console'>)</font>;
        
        <font color='#009900'>// query event mask required by input method
</font>        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> event_xim <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x11_stuff.xic<font face='Lucida Console'>)</font>
             <font color='#BB00BB'>XGetICValues</font><font face='Lucida Console'>(</font> x11_stuff.xic, XNFilterEvents, <font color='#5555FF'>&amp;</font>event_xim, <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>NULL <font face='Lucida Console'>)</font>;
        
        <font color='#BB00BB'>XSelectInput</font><font face='Lucida Console'>(</font>
            x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,
            x11_stuff.hwnd,
            StructureNotifyMask<font color='#5555FF'>|</font>ExposureMask<font color='#5555FF'>|</font>ButtonPressMask<font color='#5555FF'>|</font>ButtonReleaseMask<font color='#5555FF'>|</font>
            PointerMotionMask<font color='#5555FF'>|</font>LeaveWindowMask<font color='#5555FF'>|</font>EnterWindowMask<font color='#5555FF'>|</font>KeyPressMask<font color='#5555FF'>|</font>
            KeyReleaseMask<font color='#5555FF'>|</font> FocusChangeMask <font color='#5555FF'>|</font> event_xim
            <font face='Lucida Console'>)</font>;

        <font color='#BB00BB'>XSetWMProtocols</font><font face='Lucida Console'>(</font>
            x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,
            x11_stuff.hwnd,
            <font color='#5555FF'>&amp;</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>delete_window,
            <font color='#979000'>1</font>
            <font face='Lucida Console'>)</font>;


        <font color='#009900'>// these are just default values
</font>        x <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        y <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        width <font color='#5555FF'>=</font> <font color='#979000'>10</font>;
        height <font color='#5555FF'>=</font> <font color='#979000'>10</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>resizable <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
        <b>{</b>
            XSizeHints<font color='#5555FF'>*</font> hints <font color='#5555FF'>=</font> <font color='#BB00BB'>XAllocSizeHints</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>flags <font color='#5555FF'>=</font> PMinSize<font color='#5555FF'>|</font>PMaxSize;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>min_width <font color='#5555FF'>=</font> width;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_width <font color='#5555FF'>=</font> width;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_height <font color='#5555FF'>=</font> height; 
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>min_height <font color='#5555FF'>=</font> height; 
            <font color='#BB00BB'>XSetNormalHints</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,hints<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>hints<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    base_window::
    ~<b><a name='base_window'></a>base_window</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        <font color='#BB00BB'>close_window</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>xim <font color='#5555FF'>!</font><font color='#5555FF'>=</font> NULL<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#BB00BB'>XDestroyIC</font><font face='Lucida Console'>(</font>x11_stuff.xic<font face='Lucida Console'>)</font>;
            x11_stuff.xic <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            <font color='#BB00BB'>XFreeFontSet</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.fs<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'>delete</font> <font color='#5555FF'>&amp;</font>x11_stuff;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='close_window'></a>close_window</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
        <b>{</b>
            has_been_destroyed <font color='#5555FF'>=</font> <font color='#979000'>true</font>;

            x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_table.<font color='#BB00BB'>destroy</font><font face='Lucida Console'>(</font>x11_stuff.hwnd<font face='Lucida Console'>)</font>;           

            <font color='#BB00BB'>XDestroyWindow</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd<font face='Lucida Console'>)</font>;
            x11_stuff.hwnd <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
            x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_close_signaler.<font color='#BB00BB'>broadcast</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
        <b>}</b>   
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>bool</u></font> base_window::
    <b><a name='is_closed'></a>is_closed</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>return</font> has_been_destroyed;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_title'></a>set_title</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> title_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_mbstring_to_wstring</font><font face='Lucida Console'>(</font>title_<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_title'></a>set_title</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> ustring<font color='#5555FF'>&amp;</font> title_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>set_title</font><font face='Lucida Console'>(</font><font color='#BB00BB'>convert_utf32_to_wstring</font><font face='Lucida Console'>(</font>title_<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_title'></a>set_title</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::wstring<font color='#5555FF'>&amp;</font> title_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#009900'>// I'm pretty sure the pointer won't be modified even though
</font>        <font color='#009900'>// it isn't const anymore.
</font>        <font color='#0000FF'><u>wchar_t</u></font> <font color='#5555FF'>*</font>title <font color='#5555FF'>=</font> <font color='#0000FF'>const_cast</font><font color='#5555FF'>&lt;</font><font color='#0000FF'><u>wchar_t</u></font> <font color='#5555FF'>*</font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>title_.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        XTextProperty property;
        <font color='#BB00BB'>XwcTextListToTextProperty</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,<font color='#5555FF'>&amp;</font>title,<font color='#979000'>1</font>,XStdICCTextStyle, <font color='#5555FF'>&amp;</font>property<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XSetWMName</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,<font color='#5555FF'>&amp;</font>property<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>property.value<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='show'></a>show</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>    
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#BB00BB'>XMapRaised</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='wait_until_closed'></a>wait_until_closed</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
            x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>window_close_signaler.<font color='#BB00BB'>wait</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='hide'></a>hide</b> <font face='Lucida Console'>(</font>
    <font face='Lucida Console'>)</font>    
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#BB00BB'>XUnmapWindow</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_size'></a>set_size</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>int</u></font> width_,
        <font color='#0000FF'><u>int</u></font> height_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;


        <font color='#009900'>// do some sanity checking on these values
</font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>width_ <font color='#5555FF'>&lt;</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
            width_ <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>height_ <font color='#5555FF'>&lt;</font> <font color='#979000'>1</font><font face='Lucida Console'>)</font>
            height_ <font color='#5555FF'>=</font> <font color='#979000'>1</font>;

        width <font color='#5555FF'>=</font> width_;
        height <font color='#5555FF'>=</font> height_;
        has_been_resized <font color='#5555FF'>=</font> <font color='#979000'>true</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>resizable <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
        <b>{</b>
            XSizeHints<font color='#5555FF'>*</font> hints <font color='#5555FF'>=</font> <font color='#BB00BB'>XAllocSizeHints</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>flags <font color='#5555FF'>=</font> PMinSize<font color='#5555FF'>|</font>PMaxSize;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>min_width <font color='#5555FF'>=</font> width;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_width <font color='#5555FF'>=</font> width;
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>max_height <font color='#5555FF'>=</font> height; 
            hints<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>min_height <font color='#5555FF'>=</font> height; 
            <font color='#BB00BB'>XSetNormalHints</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,hints<font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>hints<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#BB00BB'>XResizeWindow</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,width,height<font face='Lucida Console'>)</font>;
        
        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_pos'></a>set_pos</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>long</u></font> x_,
        <font color='#0000FF'><u>long</u></font> y_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;


        x <font color='#5555FF'>=</font> x_;
        y <font color='#5555FF'>=</font> y_;

        has_been_moved <font color='#5555FF'>=</font> <font color='#979000'>true</font>;

        <font color='#BB00BB'>XMoveWindow</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,x,y<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='get_pos'></a>get_pos</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> x_,
        <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> y_
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        x_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        y_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#009900'>// we can't really trust the values we have for x and y because some window managers
</font>        <font color='#009900'>// will have reported bogus values back in the ConfigureNotify event.  So just to be
</font>        <font color='#009900'>// on the safe side we will use XTranslateCoordinates() 
</font>        <font color='#0000FF'><u>int</u></font> rx, ry;
        Window desktop_window <font color='#5555FF'>=</font> <font color='#BB00BB'>DefaultRootWindow</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
        Window junk;
        <font color='#BB00BB'>XTranslateCoordinates</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,desktop_window,<font color='#979000'>0</font>,<font color='#979000'>0</font>,<font color='#5555FF'>&amp;</font>rx, <font color='#5555FF'>&amp;</font>ry, <font color='#5555FF'>&amp;</font>junk<font face='Lucida Console'>)</font>;
        x_ <font color='#5555FF'>=</font> rx;
        y_ <font color='#5555FF'>=</font> ry;
        x <font color='#5555FF'>=</font> rx;
        y <font color='#5555FF'>=</font> ry;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='get_size'></a>get_size</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> width_,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> height_
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        width_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        height_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;


        width_ <font color='#5555FF'>=</font> width;
        height_ <font color='#5555FF'>=</font> height;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='get_display_size'></a>get_display_size</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> width_,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&amp;</font> height_
    <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>M</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        width_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        height_ <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#0000FF'><u>int</u></font> screen_number <font color='#5555FF'>=</font> <font color='#BB00BB'>XScreenNumberOfScreen</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>screen<font face='Lucida Console'>)</font>;
        width_ <font color='#5555FF'>=</font> <font color='#BB00BB'>DisplayWidth</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, screen_number<font face='Lucida Console'>)</font>;
        height_ <font color='#5555FF'>=</font> <font color='#BB00BB'>DisplayHeight</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp, screen_number<font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='invalidate_rectangle'></a>invalidate_rectangle</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> rectangle<font color='#5555FF'>&amp;</font> rect
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>is_mapped <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>rect.<font color='#BB00BB'>is_empty</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>false</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> <font color='#5555FF'>!</font>has_been_destroyed<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> x <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>left</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>long</u></font> y <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> width <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>width</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'>const</font> <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> height <font color='#5555FF'>=</font> rect.<font color='#BB00BB'>height</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            
            <font color='#BB00BB'>XClearArea</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp,x11_stuff.hwnd,x,y,width,height,<font color='#979000'>1</font><font face='Lucida Console'>)</font>;
            <font color='#BB00BB'>XFlush</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>disp<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'><u>void</u></font> base_window::
    <b><a name='set_im_pos'></a>set_im_pos</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'><u>long</u></font> x,
        <font color='#0000FF'><u>long</u></font> y
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> gui_core_kernel_2_globals;
        auto_mutex <font color='#BB00BB'>a</font><font face='Lucida Console'>(</font>wm<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>has_been_destroyed <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>true</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font>;

        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>x11_stuff.xic <font color='#5555FF'>|</font><font color='#5555FF'>|</font> <font color='#5555FF'>!</font><font face='Lucida Console'>(</font>x11_stuff.globals<font color='#5555FF'>-</font><font color='#5555FF'>&gt;</font>xim_style <font color='#5555FF'>&amp;</font> XIMPreeditPosition<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font> <font color='#0000FF'>return</font>;

        XVaNestedList   xva_nlist;
        XPoint          xpoint;

        xpoint.x <font color='#5555FF'>=</font> x;
        xpoint.y <font color='#5555FF'>=</font> y;

        xva_nlist <font color='#5555FF'>=</font> <font color='#BB00BB'>XVaCreateNestedList</font><font face='Lucida Console'>(</font><font color='#979000'>0</font>, XNSpotLocation, <font color='#5555FF'>&amp;</font>xpoint, <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>NULL<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XSetICValues</font><font face='Lucida Console'>(</font>x11_stuff.xic, XNPreeditAttributes, xva_nlist, <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> <font color='#0000FF'><u>void</u></font><font color='#5555FF'>*</font><font face='Lucida Console'>)</font>NULL<font face='Lucida Console'>)</font>;
        <font color='#BB00BB'>XFree</font><font face='Lucida Console'>(</font>xva_nlist<font face='Lucida Console'>)</font>;
    <b>}</b>

<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>#endif</font> <font color='#009900'>// POSIX
</font>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_GUI_CORE_KERNEL_2_CPp_
</font>

</pre></body></html>